package com.atguigu.gmall.realtime.util;

import com.atguigu.gmall.realtime.common.Constant;
import lombok.SneakyThrows;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Archie
 * @date 2021-10-17 16:09
 * @description
 */
public class AtguiguUtil {

	public static <T> List<T> toList(Iterable<T> it) {
		List<T> list = new ArrayList<>();
		for (T t : it) {
			list.add(t);
		}
		return list;
	}

	@SneakyThrows
	public static long toTs(String createTime, String... format) {
		String targetFormat = "";
		if (format.length == 0) {
			targetFormat = Constant.DEFAULT_DATE_FORMAT;
		} else if (format.length == 1) {
			targetFormat = format[0];
		}
		return new SimpleDateFormat(targetFormat).parse(createTime).getTime();
	}

	public static String toDataTimeString(long ts) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ts);
	}
}
